<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <!--搜索框-->
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <!--搜索框/-->

            <!--分页数据-->
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="访问链接" align="center"></el-table-column>
                <el-table-column prop="priority" label="排序号" align="center"></el-table-column>
                <el-table-column prop="description" label="描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <!--scope.row代表当前行的对象-->
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页数据/-->

            <!--分页条-->
            <div class="pagination-container">
                <el-pagination
                        background
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!--分页条/-->

            <!-- 新增标签弹层 -->
            <div class="add-form"><!--v-if="dialogFormVisible" :visible.sync="dialogFormVisible"-->
                <el-dialog v-if="dialogFormVisible" :visible.sync="dialogFormVisible" title="新增">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="菜单链接" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="path">
                                    <el-input v-model="formData.path"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="优先级" prop="priority">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单图标" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="父级菜单">
                                    <el-select v-model="formData.parentMenuId">
                                        <el-option v-for="menu in menuList" :value="menu.id"
                                                   :label="menu.name"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="描述">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible=false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog v-if="dialogFormVisible4Edit" title="编辑" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="菜单链接" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单图标" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            showDialog: true, //弹框是否为新表单
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            menuList: [],   // 父级菜单
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [
                    {required: true, message: '请输入名字', trigger: 'blur'},
                    {min: 1, max: 10, message: '请输入名字,长度在 1 到 10 个字符', trigger: 'blur'}
                ],
                // linkUrl: [{required: true, message: '请输入与链接地址', trigger: 'blur'}],
                path: [{required: true, message: '请输入访问路径', trigger: 'blur'}],
                priority: [{required: true, message: '请输入优先级,使用1-9的数字表示', trigger: 'blur'}]
            }
        },

        //钩子函数，VUE对象初始化完成后自动执行,
        created() {
            //初始化分页查询
            this.findPage();

        },

        methods: {
            //编辑
            handleEdit() {
                //1.执行表单校验
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {//校验成功
                        //2.发送ajax请求
                        axios.post("/menu/update.do", this.formData).then((response) => {
                            //2.1隐藏表单
                            this.dialogFormVisible4Edit = false;
                            this.$message({
                                message: response.data.message,
                                type: response.data.flag === true ? 'success' : 'error'
                            });
                        }).catch((err) => {
                            this.showMessage(err);
                        }).finally(() => {
                            //此处不能重置当前页：用户体验，修改后可以立马定位改的位置
                            //2.2分页查询
                            this.findPage(1); //不重置当前页
                        });
                    } else {//表单校验失败
                        this.$message.error("表单数据校验失败");
                        return false;
                    }
                });
            },

            //添加
            handleAdd() {
                //理解$refs:参数:指定form的ref属性值
                this.$refs['dataAddForm'].validate((valid) => {
                    //alert(valid);//测试
                    if (valid) {//校验成功
                        //隐藏添加表单,this可以指代vue对象
                        this.dialogFormVisible = false;//隐藏

                        //发送ajax请求
                        axios.post('/menu/create.do', this.formData).then((response) => {
                            this.$message({
                                message: response.data.message,
                                type: response.data.flag === true ? 'success' : 'error'
                            });

                            //调用分页查询
                            this.findPage();
                        }).catch(err => { //报错执行
                            this.showMessage(err);
                        });

                    } else {//校验失败
                        //弹框
                        this.$message.error('表单数据校验失败');
                        return false;
                    }
                });
            },

            //分页查询
            findPage(isCustom) { //参数:表示是否定制当前页
                //debugger;
                //0.判断是从查询按钮调用此方法
                if (!isCustom) {//无惨:默认重置当前页
                    //将当前页码重置为1
                    this.pagination.currentPage = 1;
                }

                //console.log("hello findPage!");//测试
                //1.封装参数,当前页,每页尺寸,查询条件
                let para = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };


                //2.发送ajax请求
                axios.post("/menu/pagination.do", para).then((response) => {
                    this.pagination.total = response.data.total;
                    this.dataList = response.data.rows;//赋值表数据
                }).catch(err => {
                    console.log(err);
                }).finally();
            },

            // 重置表单
            resetForm() {
                //清除验证信息
                /*if (this.$refs['dataAddForm'] != undefined) {
                  this.$refs['dataAddForm'].resetFields();
                }*/
                //清除验证信息
                /*if (this.$refs['dataAddForm'] != undefined) {
                  this.$refs['dataAddForm'].clearValidate();
                }*/
                this.formData = {};
            },

            // 弹出添加窗口
            handleCreate() {
                //console.log("create");//测试
                this.resetForm();
                this.dialogFormVisible = true;

                // 请求后台父级菜单id
                axios.get("/menu/preCreate.do").then((response) => {
                    if (response.data.flag) {
                        this.menuList = response.data.data;
                    } else {
                        this.$message.error(response.data.message);
                    }
                });
            },

            // 弹出编辑窗口
            handleUpdate(row) {//不使用row直接回显,可能其他管理员已经删除了此条数据,故以数据库为准
                //1.设置显示编辑窗口
                this.resetForm();
                this.dialogFormVisible4Edit = true;

                //2.发送ajax,回显数据
                axios.get(`/menu/preUpdate/${row.id}.do`).then((response) => {
                    if (response.data.flag) {//查询成功
                        //设置回显数据
                        this.formData = response.data.data;

                    } else {//回显失败原因:多线程同时操作,页面数据已经不存在,故无法回显
                        this.$message.error("获取数据失败,请刷新当前页面")
                    }
                });
            },

            //切换页码
            handleCurrentChange(currentPage) {//当前页更改时回调函数
                //currentPage为新的当前页
                //1.修改当前页数据
                this.pagination.currentPage = currentPage;
                //2.调用分页查询方法
                this.findPage(1); //使用自定义当前页
            },

            // 删除
            handleDelete(row) {//row为当前行json对象

                //确认框
                this.$confirm('确认删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//点确认,回调
                    //发送ajax请求
                    axios.delete(`/menu/delete/${row.id}.do`).then((response) => {
                        this.$message({
                            message: response.data.message,
                            type: response.data.flag === true ? 'success' : 'error'
                        });
                    }).catch((err) => {//报错,打印日志
                        //调用方法显示错误信息
                        this.showMessage(err);
                    }).finally(() => {//始终执行

                        this.findPage(1);//调用分页查询

                    });

                }).catch(() => {//点取消,回调
                    //取消框
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },

            //显示报错时的信息
            showMessage(err) {
                if (err === "Error: Request failed with status code 403") {
                    this.$message.error("无访问权限");
                } else {
                    this.$message.error("未知错误");
                }
            }

        }
    });
</script>

</html>